package middleware

import (
	"git.mycaigou.com/gfyx/common/usermetadata"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
	jsoniter "github.com/json-iterator/go"
)

func HeaderUserInfoToMetadata() gin.HandlerFunc {
	return func(c *gin.Context) {
		userInfo := c.GetHeader(usermetadata.HEADER_USER_INFO)
		if userInfo == "" {
			c.Next()
			return
		}
		metaUserInfo := usermetadata.MetaUserInfo{}
		err := jsoniter.Unmarshal([]byte(userInfo), &metaUserInfo)
		if err != nil {
			stark.Logger.Error(c, "解析header user_info失败："+err.Error())
			ErrResponse(c, ykerrcode.ACCESS_DENIED, "解析header user_info失败："+err.Error())
			return
		}
		c.Set(usermetadata.HEADER_USER_INFO, metaUserInfo)
		//a := usermetadata.GetMetaUserInfo(c)
		//fmt.Println(a)
		c.Next()
	}
}
